<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <style type="text/css">
        .layui-table-cell{height:40px;line-height:40px;}
    </style>
</head>
<body>

<blockquote class="layui-elem-quote quoteBox mod_default_box">
    <form class="layui-form">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="stu_name" placeholder="请输入轮播标题" autocomplete="off" class="layui-input" />
            </div>
            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-stu-submit">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
    </form>
</blockquote>

<!--数据列表-->
<table class="layui-hide" id="test" lay-filter="stu-table"></table>

<script type="text/html" id="stu-toolbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
</script>
<script type="text/html" id="tb">
    <div class="layui-container" style="margin-left:0px;">
        <a class="layui-btn layui-btn-xs" lay-event="add">新增轮播</a>
        <!--        <a class="layui-btn layui-btn-xs" lay-event="batchAdd"><i class="layui-icon layui-icon-addition"></i>批量导入车辆</a>-->
        <!--        <a style="margin-left:30px;color: blue;text-decoration: revert;"  href="/admin/che_liang_guanli/mould/mould.xls">下载批量导入模板</a>-->
    </div>
</script>
<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="status" value="{{d.id}}" title="启用" lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }} />
</script>
<script src="/js/layui/layui.js"></script>

<script>
    layui.use('table', function(){
        var $ = layui.$;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        table.render({
            elem: '#test'
            ,id:'stu-table-id'//重载需要
            ,url:'/appshouye/selectshouye_lunbo'
            ,title:'app首页轮播表'
            ,toolbar: '#tb'
            ,cols: [[
                {field: 'id',title: '序号',type:'numbers',align: 'center'}
                ,{field:'title',align : 'center',width : 200, title: '轮播标题'}
                ,{field:'content',width : 300,align : 'center', title: '内容'}
                ,{field:'admin_id',width : 200,align : 'center', title: '作者',templet:function(res){
                        if(res.admin){
                            return res.admin.username;
                        }else {
                            return "";
                        }
                    }}
                ,{field:'type',width : 200,align : 'center', title: '轮播类型',templet:function(res){
                        if(res.type == 0){
                            return "富文本";
                        }else {
                            return "地址";
                        }
                    }}
                , {field: 'picture',align : 'center',title: '轮播图片',width : 220,templet: '<div><img src="/upload/{{ d.image }}" style="width: 70px;height: 50px;" onclick="previewImgs(this)"></div>'}
                ,{field:'create_time',align : 'center', width : 200,title: '发布时间'}
                ,{field:'status',align : 'center', title:'是否启用',width:150, templet: '#checkboxTpl', sort: true,fixed: 'right'}
                ,{toolbar:"#stu-toolbar",align : 'center',width:180,title:"操作",fixed: 'right'}
            ]]
            ,page: true
            ,page: {
                curr: layui.data("notice_page").index
            },
            done: (res, curr, count) => {
                layui.data("notice_page", {
                    key: 'index',
                    value: curr
                });
            }
        });


        //搜索操作
        form.on('submit(search-stu-submit)',function (data) {
            table.reload('stu-table-id',{
                where:{"title":data.field.stu_name}
                ,page:{
                    curr:1
                }
            });
            return false;
        });


        //监听启用操作
        form.on('checkbox(lockDemo)', function(obj){
            var id = this.value;
            var status = obj.elem.checked;
            var zhuangtai;
            if(status){
                zhuangtai=1;
            }else{
                zhuangtai=0;
            }
            $.ajax({
                url:"/appshouye/upStatus"
                ,type:"get"
                ,data:{id:id,status:zhuangtai}
                ,contentType: 'application/json'
                ,success:function (data) {
                    if(data.code==0){
                        layer.close(layer.index);
                        layer.msg('操作成功');
                        table.reload("stu-table-id");
                    }else if(data.code==1){
                        layer.msg('操作失败');
                    }else{
                        layer.msg('没有权限');
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
        });

        table.on('tool(stu-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                window.localStorage.clear();
                localStorage.setItem("content", data.content);
                layer.open({
                    type: 2
                    ,title: "修改app轮播信息"
                    ,skin: 'layui-layer-molv'
                    ,area: ['95%', '95%']
                    ,content: (encodeURI("updateshouye_lunbo.html?id=" + data.id)) //这里content是一个普通的String
                });
            }
        });
        table.on('toolbar(stu-table)', function (obj) {
            var data = obj.data;
             if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    area: ['100%', '100%'],
                    content: 'addshouye_lunbo.html', //这里content是一个普通的String
                    end: function(){
                        location.reload();
                    }
                });
            }
        });
    });
</script>
<script>
    function previewImgs(obj) {
        var img = new Image();
        img.src = obj.src;
        //var height = img.height + 50; // 原图片大小
        //var width = img.width; //原图片大小
        var imgHtml = "<img src='" + obj.src + "' width='800px' height='500px'/>";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [800 + 'px', 550 + 'px'],// area: [width + 'px',height+'px'],  //原图显示
            shadeClose: true,
            scrollbar: false,
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }
</script>
</body>
</html>